<template>
	<view class="main_view">
		<view class="top_view">
			<img :src="urls" class="header_cls"/>
			<view class="input_cls">
				账号: <input v-model="form.username" placeholder="请输入账号" @input="inputChange()"/>
			</view>
			<view class="input_cls">
				密码: <input v-model="form.password" placeholder="请输入密码"/>
			</view>
			<button class="btn_cls" @click="login()">登录</button>
		</view>
		<view class="bottom_view">
			<text>忘记密码?</text>
			<navigator url="/pages/register/register">
				<text>去注册</text>
			</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				urls: "/static/header.jpeg",
				form: {
					username:"",
					password:""
				},
				mytimeout: null
			}
		},
		methods: {
			//登录的方法
			login(){
				this.$.ajax({
					type:"POST",
					url: "/user/login",
					data: this.form,
					success: (result) => {
						console.log("登录后的令牌：" + result);
						//存储令牌
						uni.setStorageSync('login_token', result);
						
						//发送WebSocket的注册消息
						this.ws.wsRegister();
						
						//跳转到首页
						uni.switchTab({
							url: "/pages/index/index"
						})
					}
				});
			},
			//输入框内容改变的事件 - 事件防抖
			inputChange(){
				console.log("输入框的内容:---", this.form.username);
				
				//原来的延迟器需要关闭
				if(this.mytimeout){
					//js官方clearTimeout 取消延迟器
					clearTimeout(this.mytimeout);
					this.mytimeout = null;
				}
				
				//延迟器，过了500毫秒再function一次
				this.mytimeout = setTimeout(() => {
					//去后端查询当前用户的头像
					this.$.ajax({
						url: "/user/queryUserHeader",
						isLoading: false,
						data: {
							username: this.form.username
						},
						success: (result) => {
							//显示头像
							this.urls = this.$.getImg(result);
						},
						fail: (result) => {
							//没有找到用户
							this.urls = "/static/header.jpeg";
							return true;
						}
					})
				}, 500);
			}
		}
	}
</script>

<style>
	
	.main_view {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		align-items: center;
		height: 100%;
	}
	
	.top_view {
		margin-top: 150rpx;
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		
	}
	
	.top_view > * {
		margin-bottom: 50rpx;
	}
	
	.header_cls {
		width: 200rpx;
		height: 200rpx;
		border-radius: 100rpx;
		box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
	}
	.input_cls {
		display: flex;
		justify-content: center;
		align-items: center;
	}
	
	.input_cls > input {
		margin-left: 20rpx;
		padding: 20rpx 30rpx;
		border-radius: 20rpx;
		box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
	}
	
	.btn_cls {
		width: 70%;
		background-color: #19b18b;
		color: white;
		margin-top: 40rpx;
		box-shadow: 10rpx 10rpx 10rpx gray;
	}
	
	button:active{
		position: relative;
		top: 5rpx;
		left: 5rpx;
		box-shadow: 5rpx 5rpx 10rpx gray;
	}
	
	.bottom_view {
		width: 100%;
		padding: 30rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
	}
	
	.bottom_view {
		font-weight: 900;
		color: gray;
	}
</style>
